clear
use "\\file\UsersW$\wrr15\Home\My Documents\My Files\PCC PROJECT\DATAVERSE FILES\TABLE2.dta"

// This example is based on data from:
// Havránek, T. (2015). 
// Measuring intertemporal substitution: The importance of method choices and selective reporting. 
// Journal of the European Economic Association, 13(6), 1180-1204.

// Stata 16.0
// October 1, 2019 

summ eis, detail
// keep if eis < r(p95) & eis > r(p5)

local rhsvars micro stockhold seprisk habits sepdur sepgov septrd

gen prec = 1/se
gen tstat = eis/se
gen invperstudy = 1/perstudy
xtset idstudy

preserve
foreach x of varlist `rhsvars' {
  gen `x'_se = `x' / se
}
xtreg tstat prec, fe vce(cluster idstudy)
reg tstat micro_se stockhold_se seprisk_se habits_se sepdur_se sepgov_se septrd_se prec [pweight=invperstudy], vce(cluster idstudy)
restore 

// We now reproduce the above with PCC

local rhsvars micro stockhold seprisk habits sepdur sepgov septrd

gen df = obs
gen pcc = tstat/sqrt(tstat^2+df)
gen varpcc = (1-pcc^2)/df
gen sepcc = sqrt(varpcc)
gen tpcc = pcc/sepcc
gen precisionpcc = 1/sepcc

xtreg tpcc precisionpcc, fe vce(cluster idstudy)
foreach x of varlist `rhsvars' {
				gen `x'_se = `x' / sepcc
}
reg tpcc micro_se stockhold_se seprisk_se habits_se sepdur_se sepgov_se septrd_se precisionpcc  [pweight=invperstudy], vce(cluster idstudy)

// Havranek (2015) does not include a variable for degrees of freedom (df)
// As a check whether it is okay to substitute number of observations for df
// I go back to the LS&D dataset and see if these are closely related.
clear
use "\\file\UsersW$\wrr15\Home\My Documents\My Files\PCC PROJECT\DATAVERSE FILES\TABLE1.dta"
summ df obs, detail
corr df obs
// CONCLUSION: They are. The correlation is virtually 100%.